package com.design.patterns.list.listener.multicaster;

import com.design.patterns.list.listener.event.WeatherEvent;
import com.design.patterns.list.listener.listener.WeatherListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 广播实现类
 * @author Wangzhiwen
 */
public class WeatherEventMulticaster implements EventMulticaster {

    /**
     * 聚合监听者
     */
    private List<WeatherListener> listenerList = new ArrayList<>();

    /**
     * 广播事件
     * @param event 事件对象
     */
    @Override
    public void multicastEvent(WeatherEvent event) {
        System.out.println("==========开始事件广播==========");
        listenerList.forEach(i -> i.onWeatherEvent(event));
        System.out.println("==========结束事件广播==========");
    }

    @Override
    public void addListener(WeatherListener listener) {
        listenerList.add(listener);
    }

    @Override
    public void removeListener(WeatherListener listener) {
        listenerList.remove(listener);
    }
}
